//
//  ShiXianJiZGrantListSubAllOrdersViewController.m
//  DistributorAB
//
//  Created by 李 on 2019/11/26.
//  Copyright © 2019年 主事丫环. All rights reserved.
//
#import "ShiXianJiZGrantListSubAllOrdersViewController.h"
#import "ZGrantListCell.h"
#import "ZGrantListModel.h"

#import "ZGrantCheckDetailVC.h"//补助金查看详情
#import "ZGrantDetailDealVC.h"//补助金审核处理(省级)

@interface ShiXianJiZGrantListSubAllOrdersViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *grantArray;
@end

@implementation ShiXianJiZGrantListSubAllOrdersViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"补助金申请";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = kColorWithHex(0xf2f2f2);
    
    _grantArray = [NSMutableArray array];
    
    [self setupViewAction];
    
    [self requestData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:USER_DEAL_GRANT_SUCCESS object:nil];
}
#pragma mark - 界面布局
- (void)setupViewAction{
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    //空态界面展示
    KKTableViewNoDataView *viewPlaceHolder = [[KKTableViewNoDataView alloc] initWithFrame:CGRectMake((kScreen_Width - 100) / 2.0, (kScreen_Height - 72) / 2.0, 100, 72) image:imgname(@"tableviewPlaseholder") viewClick:^{
    }];
    viewPlaceHolder.backgroundColor = kColorWithHex(0xf2f2f2);
    _tableView.placeHolderView = viewPlaceHolder;
    
    [_tableView registerClass:[ZGrantListCell class] forCellReuseIdentifier:NSStringFromClass([ZGrantListCell class])];
    
}
#pragma mark - 数据请求
- (void)requestData{
    kWeakSelf;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    if (self.type == ZGrantListSubVCTypeAll) {
        [param setObjectIfNotNil:@"" forKey:@"state"];
    }else if (self.type == ZGrantListSubVCTypeSubmited){
        [param setObjectIfNotNil:@"1" forKey:@"state"];
    }else if (self.type == ZGrantListSubVCTypeFinish){
        [param setObjectIfNotNil:@"2" forKey:@"state"];
    }
    [NetWorkTool POST:USER_GRANTS_LIST param:param success:^(id dic) {
        NSArray *grantArray = [dic objectForKeyNotNil:@"data"];
        [weakSelf.grantArray removeAllObjects];
        [grantArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ZGrantListModel *model = [ZGrantListModel mj_objectWithKeyValues:obj];
            [weakSelf.grantArray addObjectNotNil:model];
        }];
        [weakSelf.tableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [weakSelf.grantArray removeAllObjects];
        [weakSelf.tableView reloadData];
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.grantArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGrantListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZGrantListCell class]) forIndexPath:indexPath];
    ZGrantListModel *grantModel = [self.grantArray objectAtIndexSafe:indexPath.row];
    cell.grantModel = grantModel;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [ZGrantListCell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGrantListModel *grantModel = [self.grantArray objectAtIndexSafe:indexPath.row];
    if (AGENT_LEVEL == 1) {
        //省级
        if ([grantModel.state isEqualToString:@"1"]) {
            ZGrantDetailDealVC *vc = [[ZGrantDetailDealVC alloc] init];
            vc.grantID = grantModel.Id;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            ZGrantCheckDetailVC *vc = [[ZGrantCheckDetailVC alloc] init];
            vc.grantID = grantModel.Id;
            vc.isFromDeal = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else{
        //市县级
        ZGrantCheckDetailVC *vc = [[ZGrantCheckDetailVC alloc] init];
        vc.grantID = grantModel.Id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

